package com.example.day16_mp;

import android.media.MediaPlayer;
import android.widget.ImageView;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Random;

public class MusicUtils {
    //单例模式
    private MusicUtils(){}
    private static MusicUtils musicUtils = null;
    public static MusicUtils getInstance(){
        if(musicUtils==null){
            musicUtils = new MusicUtils();
        }
        return musicUtils;
    }
    //用到的参数
    private static MediaPlayer mediaPlayer = new MediaPlayer();
    //播放列表
    private List<Music.DataBean> list;
    //当前播放的下标
    private int index;
    //歌曲播放的模式 0顺序播放 1随机播放 2 单曲循环
    private int playMode;

    public List<Music.DataBean> getList() {
        return list;
    }

    public void setList(List<Music.DataBean> list) {
        this.list = list;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public int getPlayMode() {
        return playMode;
    }

    public void setPlayMode(int playMode) {
        this.playMode = playMode;
    }
    //播放歌曲
    public void play(int position){
        index = position;//赋值给全局变量
        mediaPlayer.reset();
        try {
            mediaPlayer.setDataSource(list.get(position).getData());
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //暂停和继续按钮  --点击时，图标发生变化
    public void playOrPause(ImageView imageView){
        if(mediaPlayer.isPlaying()){
            mediaPlayer.pause();
            imageView.setImageResource(R.drawable.baseline_play_circle_outline_24);//显示圆形图标，代表播放的图标
        }else{
            mediaPlayer.start();
            imageView.setImageResource(R.drawable.baseline_pause_circle_outline_24);//显示方形图标，代表暂停的图标
        }
    }
    //上一首下一首
    public void pre(){
        if(playMode==0){//顺序播放
            if(index==0){//默认播放的是第一首歌时，点击上一首，切换到最后一首歌
                index = list.size()-1;
            }else {
                index--;
            }

        }else if(playMode==1){
            index = new Random().nextInt(list.size()-1);//获取一个随机数
        }
        //单曲循环，歌曲下标不变
        play(index);
    }
    public void next(){
        if(playMode==0){//顺序播放
            if(index==list.size()-1){//默认播放的是第一首歌时，点击上一首，切换到最后一首歌
                index = 0;
            }else{
                index++;
            }

        }else if(playMode==1){
            index = new Random().nextInt(list.size()-1);//获取一个随机数
        }
        //单曲循环，歌曲下标不变
        play(index);
    }
    //单独封装播放或暂停方法--接收到系统广播的时候进行使用
    public void start(ImageView imageView){
        if(!mediaPlayer.isPlaying()){
            mediaPlayer.start();
            imageView.setImageResource(R.drawable.baseline_pause_circle_outline_24);
        }
    }
    public void pause(ImageView imageView){
        if(mediaPlayer.isPlaying()){
            mediaPlayer.pause();
            imageView.setImageResource(R.drawable.baseline_play_circle_outline_24);
        }
    }
    //快进快退
    public void jin(){
        int position = mediaPlayer.getCurrentPosition();
        mediaPlayer.seekTo(position+=5000);
    }
    public void tui(){
        int position = mediaPlayer.getCurrentPosition();
        mediaPlayer.seekTo(position-=5000);
    }
    //获取进度条的两个值
    public int getProgress(){
        return mediaPlayer.getCurrentPosition();
    }
    public int getMax(){
        return mediaPlayer.getDuration();//可以从实体类中获取，也可以从mediaplayer中获取
    }
    //进度条左右两边的文字
    public String getCur(){
        SimpleDateFormat format = new SimpleDateFormat("mm:ss");
        return format.format(mediaPlayer.getCurrentPosition());
    }
    public String getDur(){
        SimpleDateFormat format = new SimpleDateFormat("mm:ss");
        return format.format(mediaPlayer.getDuration());
    }
    //进度条拖拽事件
    public void seekTo(int time){
        mediaPlayer.seekTo(time);
    }
    //详情界面看到的歌曲名称歌手名称歌曲封面图片歌词
    public String getName(){
        return list.get(index).getName();
    }
    public String getSinger(){
        return list.get(index).getSinger();
    }

    public String getPicUrl(){
        return list.get(index).getPicUrl();
    }

    public String getLrc(){
        return list.get(index).getLrc();//歌词
    }

    //判断当前的状态
    public boolean isPlaying(){
        return mediaPlayer.isPlaying();
    }

}
